<?php

class UpdateinfoAction extends CAction
{
    public function run()
    {
		$message = '';
		
		if(!empty($_SESSION['token']['access_token'])){
			$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $_SESSION['token']['access_token'] );
			//$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $_REQUEST['viewer'] );
			$ms  = $c->home_timeline(); // done
			$uid_get = $c->get_uid();
			$uid = $uid_get['uid'];
			$user_message = $c->show_user_by_id($uid);
			//var_dump($_SESSION);
			//var_dump($_REQUEST);
			//var_dump($user_message);
			//echo '<img src="'.$user_message['profile_image_url'].'" />';
			
				$model = new User;
				$user = $model->findByPk($user_message['id']);
				
				if(!empty($user)){
					if($user->token != $_SESSION['token']['access_token'] or $user->profile_image != $user_message['profile_image_url']){
						$user->token = $_SESSION['token']['access_token'];
						$user->profile_image = $user_message['profile_image_url'];
						$user->updatetime = time();
						if($user->save())
							$message = 'update success';
					}
				
				}else{
					$model->id = $user_message['id'];
					$model->idstr = $user_message['idstr'];
					$model->screen_name = $user_message['screen_name'];
					$model->name = $user_message['name'];
					$model->profile_image = $user_message['profile_image_url'];
					$model->token = $_SESSION['token']['access_token'];
					$model->updatetime = time();
					
					if($model->save())
						$message='create success';
					
				}
		}
		
		echo '<a href="'.$this->createUrl('site/index').'">go back'.$message.'</a>';
		
		
	}
	

	
	
}